package hp.com.servlet.UsersServlet;

import hp.com.entity.Users;
import hp.com.service.UsersService;
import hp.com.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/checkEmailUpdateServlet")
public class CheckEmailUpdateServlet extends HttpServlet {
    private UsersService usersService = new UsersServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            int id = Integer.parseInt(req.getParameter("id"));
            String oldemail = req.getParameter("oldemail");
            String newemail = req.getParameter("newemail");
            if (newemail.indexOf("@")!=-1){
                Users oldUsers = new Users();
                oldUsers.setId(id);
                oldUsers.setEmail(oldemail);
                boolean b = usersService.checkOldEmail(oldUsers);
                if (b){
                    Users newUsers = new Users();
                    newUsers.setId(id);
                    newUsers.setEmail(newemail);
                    boolean b1 = usersService.updateUserEmail(newUsers);
                    if (b1){
                        req.setAttribute("emailmsg","修改成功！");
                    }else{
                        req.setAttribute("emailmsg","修改失败！");
                    }
                }else {
                    req.setAttribute("emailmsg","邮箱输入错误，请重新输入！");
                }
            }else {
                req.setAttribute("emailmsg","请输入规范的邮箱！");
            }
            req.getRequestDispatcher("easybuy/Member_Safe.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
